var express = require("express");
var router = express.Router();
const Db = require("../utils/db");
const createSql = require("../utils/Create_SQL");
const path = require('path');
const { errorCode, successCode, config } = require('../config/config');
const utils = require('../utils/utils.js');
const objectCheck = require("./novelApiCheck");
const expressWs = require('express-ws'); //及时通信
expressWs(router);

/**
 * @swagger
 * /api/admin/getNovelTile:
 *    get:
 *      tags:
 *      - 后台管理 小说管理
 *      summary: 小说管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 标题
 *          in: query
 *          required: false
 *          type: string
 *        - name: brief_introduction
 *          description: 简介
 *          in: query
 *          required: false
 *          type: string
 *        - name: start_time
 *          description: 开始时间
 *          in: query
 *          required: false
 *          type: string
 *        - name: end_time
 *          description: 结束时间
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getNovelTile", async (req, res, next) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title, brief_introduction, start_time, end_time } = req.query;
    let where = "";
    if (title) {
        where += `title like '%${title}%'`;
    }
    if (brief_introduction) {
        where = where ? where + ` and brief_introduction like '%${brief_introduction}%'` : `brief_introduction like '%${brief_introduction}%'`;
    }
    if (start_time && end_time) {
        where = where ? where + ` and addtime between '${start_time}' and '${end_time}'` : `addtime between '${start_time}' and '${end_time}'`;
    }
    if(start_time && !end_time){
        where = where ? where + ` and addtime >= '${start_time}'` : `addtime >= '${start_time}'`;
    }
    if(!start_time && end_time){
        where = where ? where + ` and addtime <= '${end_time}'` : `addtime <= '${end_time}'`;
    }
    where = where ? where + ' and is_del = 0' : 'is_del = 0';
    let sql = createSql.select("novel").where(where).limit(pageSize, (page - 1) * pageSize).order("addtime desc").end();
    let sqlCount = createSql.select("count(*) as count").from("novel").where(where).end();
    let reslut = await Db.query(sql , res);
    let count = await Db.query(sqlCount , res);
    if(reslut.code || count.code) return;
    res.json({
        ...successCode[200],
        data: {
            data: count[0].count,
            list: reslut
        }
    });
});


module.exports = router;
